﻿#region << 版 本 注 释 >>
/*----------------------------------------------------------------
// Copyright (C) 2021 kingerrobot
// 版权所有。 
//
// 文件名：BeforeWarehousingImpl
// 文件功能描述：入库前扫描输送任务
//
// 创建者：名字 (zzl)
// 时间：2021/12/6 15:01:56
//
// 修改人：
// 时间：
// 修改说明：
// 版本：V1.0.0
//----------------------------------------------------------------*/
#endregion


using K8.CBB;
using K8.MDCBB;
using KingerRobot.MexicoSailunMDC.Dao;
using KingerRobot.MexicoSailunMDC.Spi;
using KingerRobot.MexicoSailunMDC.SpiService;
using System;
using System.Diagnostics;
using System.Drawing.Imaging;
using System.Security.Cryptography;
using System.Threading.Tasks;
using static K8.CBB.XID;

namespace KingerRobot.MexicoSailunMDC.SpiService;
[Logging]
public class WarehousingBeforeImpl : IWarehousingBeforeService
{
    private readonly static ITask _Task = new BTask();
    private readonly static ITaskCmd _TaskCmd = new BTaskCmd();
    private readonly static IWcsAsrsNetInput _WcsAsrsNetInput = new BWcsAsrsNetInput();
    private readonly static IWcsAsrsCrnInput _WcsAsrsCrnInput = new BWcsAsrsCrnInput();
    private readonly static IWcsLocSku _WcsLocSku = new BWcsLocSku();
    private readonly static IProdSku _ProSku = new BProdSku();
    private readonly static IAbnormal _Abnormal = new BAbnormal();
    private readonly static IWmsSetting _WmsSetting = new BWmsSetting();
    private readonly static IProdMaterial _ProdMaterial = new BProdMaterial();
    private readonly static IWmsOutOrderSummary _WmsOutOrderSummary = new BWmsOutOrderSummary();
    private readonly static IWms2MesService _Wms2MesService = new Wms2MesImpl();
    private readonly static IRcsEquipCuring _RcsEquipCuring = new BRcsEquipCuring();
    private readonly static IProdProduct _ProdProduct = new BProdProduct();
    private readonly static IBakTask _BakTask = new BBakTask();
    private readonly static IWcsWarehouseCrnMaterial _WcsWarehouseCrnMaterial = new BWcsWarehouseCrnMaterial();
    private readonly static IWcsAsrsNetOutPut _WcsAsrsNetOutPut = new BWcsAsrsNetOutPut();
    private readonly static IRcsLocation _RcsLocation = new BRcsLocation();
    private readonly static object _Lock1 = new object();
    private readonly static object _LockDelivery = new object();
    private readonly static IWcsScanner _WcsScanner = new BWcsScanner();
    private readonly static IMesRpc _MesRpc = new BMesRpc();
    private readonly static IMesProduct _MesProduct = new BMesProduct();
    private readonly static IRcsEquipScanner _RcsEquipScanner = new BRcsEquipScanner();

    public long CreateTask(long locId)
    {

        var remark = $"条已执行任务创建111";
        var scanner = _WcsScanner.GetFirstNoTaskWcsScannerByStartLoc(locId);
        if (scanner is null)
        {
            return 0;
        }


        var scannerList = new List<POWcsScanner>() { scanner };
        var result = 1;

        foreach (var item in scannerList)
        {
            remark = $"条码{item.Barcode},已执行任务创建";
            WriteLog(remark, true);
            _WcsScanner.UpdateWcsScannerStatusById(item.Id ?? 0, (int)EnumScannerStatus.Creating, remark);
            long taskId = 0;
            try
            {
                taskId = CreateTaskV2(item.StartLoc ?? 0, item.Barcode);
            }
            catch (Exception ex)
            {
                _WcsScanner.UpdateWcsScannerStatusById(item.Id ?? 0, (int)EnumScannerStatus.NoCreate, remark);
                WriteLog($"入库异常-{ex.ToString()}", true);
            }
            //如果任务创建失败 直接跳出循环 防止给后面的条码生成任务
            if (taskId <= 0)
            {

                if (item.RemarkMemo.Length < 900)
                {
                    remark = $"{item.RemarkMemo}--条码{item.Barcode},任务创建失败,返回值{taskId},{DateTime.Now.ToShortTimeString()},已初始化任务状态";
                }
                _WcsScanner.UpdateWcsScannerStatusById(item.Id ?? 0, (int)EnumScannerStatus.NoCreate, remark);
                if (item.ErrorCode == 0)//只有第一次失败才更新失败状态
                {
                    _WcsScanner.UpdateWcsScannerErrorById(item.Id ?? 0, taskId);
                }
                break;
                //continue;
            }
            remark = $"{item.RemarkMemo}--条码{item.Barcode},任务已成功创建,{DateTime.Now.ToShortTimeString()}";
            _WcsScanner.UpdateWcsScannerStatusById(item.Id ?? 0, (int)EnumScannerStatus.Created, remark, taskId);
        }
        return result;
        //}
    }


    public long CreateTaskV2(long scannerLocId, string productCode)
    {
        var abnormalTaskId = 0L;
        var abnormalRemark = "";
        if (productCode.Contains(ConstFields.NoRead))
        {
            return -16;
            //abnormalRemark = $"Noread下异常";
            ////送往异常
            //(abnormalTaskId, _) = _Abnormal.CreateAbnormal(productCode ?? string.Empty, 0, scannerLocId, abnormalRemark);
            //return abnormalTaskId;
        }

        var task = _Task.GetTaskByProductCode(productCode);
        WriteLog($"条码{productCode},查找任务{task is not null}", true);
        if (task is not null)
        {

            //如果是三号扫描 则判断是否生成过指令
            if (scannerLocId == (int)EnumScannerLoc.Three)
            {
                WriteLog($"开始执行三号扫码二次扫描,条码{productCode}有任务", true);
                //判断是否在1/2号扫描就生成任务 如果生成则直接用找到的堆垛机
                var netInput = _WcsAsrsNetInput.GetAsrsNetInputByStartAndTargetLoc(task.StartLoc ?? 0, task.TargetLoc ?? 0);
                if (netInput != 0)
                {
                    //备份删除上段指令
                    _TaskCmd.BakAndDeleteCmd(task.Id ?? 0);
                    //创建新指令
                    _TaskCmd.CreateTaskCmd(new()
                    {
                        TaskId = task.Id,
                        TaskNo = task.TaskNo,
                        TaskType = task.TaskType,
                        EquipId = task.StartEquip,
                        StartLoc = scannerLocId,
                        CurrentLoc = scannerLocId,
                        TargetLoc = task.TargetLoc,
                        CmdStatus = (int)EnumTaskCmdStatus.Executable,
                        CmdType = (int)EnumTaskType.Transportation,
                    });
                    WriteLog($"开始执行三号扫码二次扫描,条码{productCode},返回值{task.Id ?? 0}", true);
                    return task.Id ?? 0;
                }
                else
                {
                    _Task.DeleteTask(task.Id ?? 0);
                    return 0;
                }
            }
            else
            {

                var cmd = _TaskCmd.GetTaskCmdByTaskId(task?.Id ?? 0);
                if (cmd is not null)
                {
                    if (cmd.StartLoc == scannerLocId)
                    {
                        if (cmd.CmdStatus == (int)EnumTaskCmdStatus.Executable)
                        {
                            return task?.Id ?? 0;
                        }
                        _Task.DeleteTask(task.Id ?? 0);
                        return -14;
                    }
                }
            }

        }
        var productTaskId = task?.Id ?? 0;
        //现在本地mes_product读取胎胚信息
        var tireInfo = _MesProduct.GetInfoByBarcode(productCode);
        ////如果本地没有 再去mes中间表读取
        //if (tireInfo is null)
        //{
        //    var mesService = K8.RSF.Client.Create<IMesService>();
        //    tireInfo = mesService.GetTireInfos(productCode)?.TireInfo;
        //}


        if (tireInfo is null)
        {
            WriteLog($"胎胚：{productCode},未找到胎胚信息", true);

            //if (productCode.Contains("NOREAD")) 
            //{
            //    return -16;
            //}
            //送往异常
            abnormalRemark = $"胎胚：{productCode},未找到胎胚信息，下异常";
            WriteLog(abnormalRemark, true);
            (abnormalTaskId, _) = _Abnormal.CreateCircularAbnormal(productCode ?? string.Empty, 0, scannerLocId, abnormalRemark);
            //return -15;
            return abnormalTaskId;
        }

        if (tireInfo.TbmExpirationTime < DateTime.Now)
        {
            //送往异常
            abnormalRemark = $"胎胚：{productCode},超期，下异常";
            WriteLog(abnormalRemark, true);
            (abnormalTaskId, _) = _Abnormal.CreateCircularAbnormal(productCode ?? string.Empty, 0, scannerLocId, abnormalRemark);
            return abnormalTaskId;
        }
        //if (tireInfo.TireLock ==1)
        //{
        //    //送往异常
        //    abnormalRemark = $"胎胚：{productCode},锁定，下异常";
        //    WriteLog(abnormalRemark, true);
        //    (abnormalTaskId, _) = _Abnormal.CreateAbnormal(productCode ?? string.Empty, 0, scannerLocId, abnormalRemark);
        //    return abnormalTaskId;
        //}

        if (!tireInfo.InPermit.Equals("Y"))
        {
            //送往异常
            abnormalRemark = $"胎胚：{productCode},不允许入库，下异常";
            WriteLog(abnormalRemark, true);
            (abnormalTaskId, _) = _Abnormal.CreateCircularAbnormal(productCode ?? string.Empty, 0, scannerLocId, abnormalRemark);
            return abnormalTaskId;
        }
        //暂时注释
        if (tireInfo.TireLock == 1)
        {
            //送往异常
            abnormalRemark = $"胎胚：{productCode},锁定，下异常";
            WriteLog(abnormalRemark, true);
            (abnormalTaskId, _) = _Abnormal.CreateCircularAbnormal(productCode ?? string.Empty, 0, scannerLocId, abnormalRemark);
            return abnormalTaskId;
        }

        var crnInput = _WcsAsrsCrnInput.GetWcsAsrsCrnInputs();

        if (crnInput is null || !crnInput.Any())
        {
            WriteLog("没有可用的入库站台");

            return -7;
        }

        //var netInput = _WcsAsrsNetInput.GetNetInputByStartLoc(locId);
        //if (netInput is null || !crnInput.Any())
        //{
        //    WriteLog("没有可用的入库路线");

        //    return -8;
        //}

        var taskInfoBean = new TaskInfoBean();
        ProdProductBindingBean product;




        long taskId;
        // 创建绑定关系
        (taskId, product) = GetProductInfo(tireInfo, scannerLocId, task);
        var skuId = product.POProdSku?.Id ?? 0;
        DateTime dateTime = DateTime.Now;
        var materialCode = tireInfo.MaterialCode;
        var materialId = tireInfo.MaterialId;

        // 直运
        //var isDirectDelivery = _RcsEquipScanner.GetRcsEquipScannerByLocId(scannerLocId)?.IsDirectDelivery ?? 0;
        //if (isDirectDelivery == 1)
        //{
        //    lock (_LockDelivery)
        //    {
        //        var directDeliveryTaskId = DirectDeliveryTask(scannerLocId, materialId, skuId, productCode);
        //        if (directDeliveryTaskId > 0)
        //        {
        //            return directDeliveryTaskId;
        //        }
        //    }

        //}




        var material = _ProdMaterial.GetMaterialByCode(materialCode);
        (long targetCrnLoc, int groupNo, int isNotPair) = GetTargetLocM(scannerLocId, materialCode, material?.Id ?? 0, productCode);

        if (targetCrnLoc == -1 || targetCrnLoc == -3
            || targetCrnLoc == 0
            || targetCrnLoc == -4 || targetCrnLoc == -5)//  || targetCrnLoc == -2 || targetCrnLoc == -6|| targetCrnLoc == -7
        {
            (abnormalTaskId, product) = _Abnormal.CreateCircularAbnormal(productCode, targetCrnLoc, scannerLocId, $"未找到目标位原因：{GetScannerTargetAbnormal(targetCrnLoc)}");
            return abnormalTaskId;
        }
        WriteLog($"条码{productCode}开始创建任务305", true);
        var remark = string.Empty;
        //没货位了
        if (targetCrnLoc == -6 || targetCrnLoc == -7)
        {
            WriteLog($"条码{productCode}--{targetCrnLoc}没货位了", true);
            return targetCrnLoc;
        }
        //
        if (targetCrnLoc == -8)
        {
            WriteLog($"条码{productCode}--{targetCrnLoc}没入库路线1", true);
            return targetCrnLoc;
        }
        //没出库路线
        if (targetCrnLoc == -2)
        {
            WriteLog($"条码{productCode}--{targetCrnLoc}没出库路线2", true);
            return targetCrnLoc;
        }
        WriteLog($"条码{productCode}开始创建任务306", true);
        // 没有满足条件输送路线 （进异常）
        if (targetCrnLoc <= 0)
        {
            (abnormalTaskId, product) = _Abnormal.CreateCircularAbnormal(productCode, productTaskId, scannerLocId, $"{scannerLocId},未找到目标位原因：{GetScannerTargetAbnormal(targetCrnLoc)}");

            return abnormalTaskId;
        }
        WriteLog($"条码{productCode}开始创建任务307", true);
        // 判断从开始到目标有没有缓存位
        var cacheLoc = _WcsAsrsNetInput.GetAsrsNetInputByStartLocTargetLoc(scannerLocId, targetCrnLoc);
        var pairStr = string.Empty;
        if (isNotPair != 0)
        {
            pairStr = isNotPair == -1 ? $"{isNotPair}没有配对" : isNotPair < 0 ? $"{isNotPair}已配对" : $"{isNotPair}配对输送导致缓存数超限,所以重新查找目标为[{targetCrnLoc}]";
        }
        WriteLog($"条码{productCode}开始创建任务308", true);


        // Task表中添加task_group值，并且将原来的输送线任务改为了入库任务
        taskInfoBean = _Task.CreateTaskAndCmdGroup(new()
        {
            StartLoc = scannerLocId,
            TargetLoc = targetCrnLoc,
            TargetCmdLoc = cacheLoc == 0 ? targetCrnLoc : cacheLoc,
            Type = (int)EnumTaskType.Transportation,
            GroupNo = groupNo,
            SkuId = skuId,
            ScanCount = 1,
            StartTaskType = 1,
            Remark = $"【创建{_Lock1.GetHashCode()}】| {pairStr}| 条码{productCode}[{dateTime.ToString("yyyy-MM-dd HH:mm:ss.fff")} ~ {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}]| {remark}",
        });

        WriteLog($"条码{productCode}开始创建任务369", true);
        //记录扫描的物料跟时间 无用 暂时注释
        //_RcsEquipBuilding.AddLineMaterialAndTime(materialId, product.POProdProduct.MdEquipId ?? 0);
        return taskInfoBean?.POWcsTask?.Id ?? 0;



    }


    public long CreatePairTask(long scannerLocId, string productCodeA, string productCodeB)
    {
        var abnormalTaskIdA = 0L;
        var abnormalRemarkA = "";
        var abnormalTaskIdB = 0L;
        var abnormalRemarkB = "";
        //if (productCodeB.Contains(ConstFields.NoRead))
        //{
        //    //return -16;
        //    abnormalRemarkA = $"B位置{productCodeB},{productCodeA}下异常";
        //    //送往异常
        //    (abnormalTaskIdA, _) = _Abnormal.CreateAbnormal(productCodeA ?? string.Empty, 0, scannerLocId, abnormalRemarkA);
        //    return abnormalTaskIdA;
        //}

        if (productCodeB.Contains(productCodeA))
        {
            //return -16;
            WriteLog($"AB条码一致{productCodeA}", true);
            return 0;
        }
        //if (isAbnormal)
        //{
        //    //return -17;

        //    WriteLog($"下一条配对胎胚为Noread,此胎配下异常", true);
        //    abnormalRemark = $"下一条配对胎胚为Noread,此胎配下异常";
        //    //送往异常
        //    (abnormalTaskId, _) = _Abnormal.CreateAbnormal(productCodeA ?? string.Empty, 0, scannerLocId, abnormalRemark);
        //    return abnormalTaskId;
        //}


        var taskA = _Task.GetTaskByProductCode(productCodeA);

        var taskB = new POWcsTask();
        if (!productCodeB.Contains(ConstFields.NoRead)  || !string.IsNullOrEmpty(productCodeB))
        {
            taskB = _Task.GetTaskByProductCode(productCodeB);
        }
        WriteLog($"条码{productCodeA},查找任务{taskA is not null}", true);
        if (taskA is not null)
        {
            //如果当前扫码头是3号扫码 并且任务的开始位置不是3号扫码 则删除对应任务
            if (taskA.StartLoc != scannerLocId && scannerLocId == (int)EnumScannerLoc.Three)
            {
                _Task.DeleteTask(taskA.Id ?? 0);
            }
            ////如果当前扫码头是2号扫码 并且任务的开始位置不是2号扫码 则删除对应任务
            else if (taskA.StartLoc != scannerLocId && scannerLocId == (int)EnumScannerLoc.Two)
            {
                _Task.DeleteTask(taskA.Id ?? 0);
            }
            else
            {

                //判断任务是否是在A位置生成
                if (taskA.PairStatus == (int)EnumPairStatus.Pairing)
                {
                    return taskA.Id ?? 0;
                }
                else
                {
                    return -14;
                    //如果有任务 直接删除原有任务
                    _Task.DeleteTask(taskA.Id ?? 0);

                    //var cmd = _TaskCmd.GetTaskCmdByTaskId(taskA?.Id ?? 0);
                    //if (cmd is not null)
                    //{
                    //    if (cmd.StartLoc == scannerLocId)
                    //    {
                    //        if (cmd.CmdStatus == (int)EnumTaskCmdStatus.Executable)
                    //        {
                    //            return taskA?.Id ?? 0;
                    //        }
                    //        _Task.DeleteTask(taskA.Id ?? 0);
                    //        return -14;
                    //    }
                    //}
                }
            }


        }
        //B位置如果有任务 直接删除
        if (!productCodeB.Contains(ConstFields.NoRead) && taskB is not null)
        {
            _Task.DeleteTask(taskB.Id ?? 0);
        }


        var productTaskId = taskA?.Id ?? 0;
        //现在本地mes_product读取胎胚信息
        var tireInfoA = _MesProduct.GetInfoByBarcode(productCodeA);
        var tireInfoB = new TireInfo();
        if (!productCodeB.Contains(ConstFields.NoRead))
        {
            tireInfoB = _MesProduct.GetInfoByBarcode(productCodeB);
        }
        ////如果本地没有 再去mes中间表读取
        //if (tireInfo is null)
        //{
        //    var mesService = K8.RSF.Client.Create<IMesService>();
        //    tireInfo = mesService.GetTireInfos(productCode)?.TireInfo;
        //}


        if (productCodeB.StartsWith(ConstFields.Default) ||  string.IsNullOrEmpty(productCodeB))
        {
            //判断A位置是否超时 

            var scanner = _WcsScanner.GetWcsScannerByProduct(scannerLocId, productCodeA);
            if (scanner is null)
            {
                return -17;
            }
            //return -17;
            var wmsSetting = _WmsSetting.GetWmsSettingByCode(ConstFields.ScannerTimeOut);
            int.TryParse(wmsSetting?.Value, out int timeOut);

            timeOut = timeOut == 0 ? 60 : timeOut;

            DateTime? dt = scanner.CreateTime == null ? DateTime.Now : scanner.CreateTime;
            //var outTime = (scanner.CreateTime ?? DateTime.Now).AddSeconds(timeOut);
            var outTime = dt?.AddSeconds(timeOut);
            //如果超时 创建单胎任务
            if (DateTime.Now > outTime)
            {
                //return -17;
                WriteLog($"胎胚：{productCodeA},A位置是否超时,now{DateTime.Now},outTime{outTime},timeOut{timeOut} ", true);
                //var materialInfoA = tireInfoA.MaterialCode;
                //var materialInfoB = tireInfoB == null ? tireInfoB.MaterialCode : "";
                if (tireInfoA is null)
                {
                    if (productCodeA.Contains(ConstFields.NoRead))
                    {
                        return 0;
                    }
                    WriteLog($"胎胚：{productCodeA},未找到胎胚信息1111", true);
                    //送往异常
                    abnormalRemarkA = $"胎胚：{productCodeA},未找到胎胚信息，下异常";
                    WriteLog(abnormalRemarkA, true);
                    (abnormalTaskIdA, _) = _Abnormal.CreateCircularAbnormal(productCodeA ?? string.Empty, 0, scannerLocId, abnormalRemarkA);
                    //return -15;
                    return abnormalTaskIdA;
                }
                else
                {
                    if (tireInfoA.TireLock == 1)
                    {
                        //送往异常
                        abnormalRemarkA = $"胎胚：{productCodeA},单胎锁定，下异常";
                        WriteLog(abnormalRemarkA, true);
                        (abnormalTaskIdA, _) = _Abnormal.CreateCircularAbnormal(productCodeA ?? string.Empty, 0, scannerLocId, abnormalRemarkA);
                        return abnormalTaskIdA;
                    }
                    if (tireInfoA.TbmExpirationTime < DateTime.Now)
                    {
                        //送往异常
                        abnormalRemarkA = $"胎胚：{productCodeA},单胎超期，下异常";
                        WriteLog(abnormalRemarkA, true);
                        (abnormalTaskIdA, _) = _Abnormal.CreateCircularAbnormal(productCodeA ?? string.Empty, 0, scannerLocId, abnormalRemarkA);
                        return abnormalTaskIdA;
                    }
                    if (!tireInfoA.InPermit.Equals("Y"))
                    {
                        //送往异常
                        abnormalRemarkA = $"胎胚：{productCodeA},单胎不允许入库，下异常";
                        WriteLog(abnormalRemarkA, true);
                        (abnormalTaskIdA, _) = _Abnormal.CreateCircularAbnormal(productCodeA ?? string.Empty, 0, scannerLocId, abnormalRemarkA);
                        return abnormalTaskIdA;
                    }
                    var materialInfoA = tireInfoA.MaterialCode;
                    var noPariTaskId = CreateNoPairTask(scannerLocId, materialInfoA, "", productCodeA, productCodeB, tireInfoA);
                    return noPariTaskId;
                }
            }
            //如果不超时 等待
            else
            {
                return -17;
            }

        }

        if (tireInfoA is null || tireInfoB is null || productCodeA.Contains(ConstFields.NoRead) || productCodeB.Contains(ConstFields.NoRead) || productCodeB.Contains(ConstFields.Default))
        {

            WriteLog($"胎胚：{productCodeA}或{productCodeB},未找到胎胚信息", true);
            //如果A是空 生成a异常
            if (tireInfoA is null)
            {
                //两条胎的第二条胎Noread 走到A位置的时候 不生成异常
                if (productCodeA.Contains(ConstFields.NoRead))
                {
                    return 0;
                }
                WriteLog($"胎胚：{productCodeA},未找到胎胚信息1111", true);
                //送往异常
                abnormalRemarkA = $"胎胚：{productCodeA},未找到胎胚信息，下异常";
                WriteLog(abnormalRemarkA, true);
                (abnormalTaskIdA, _) = _Abnormal.CreateCircularAbnormal(productCodeA ?? string.Empty, 0, scannerLocId, abnormalRemarkA);
                //return -15;
                return abnormalTaskIdA;
            }
            //如果b是noread 或者b为空 给A生成堆垛机任务
            else
            {
                //if (!productCodeB.Contains(ConstFields.NoRead))
                //{ 
                WriteLog($"胎胚：{productCodeB},未找到胎胚信息2222", true);
                var materialInfoA = tireInfoA.MaterialCode;
                //var materialInfoB = tireInfoB == null ? tireInfoB.MaterialCode : "";
                var noPariTaskId = CreateNoPairTask(scannerLocId, materialInfoA, "", productCodeA, productCodeB, tireInfoA);
                return noPariTaskId;
                //}
            }


        }

        if (tireInfoA.TbmExpirationTime < DateTime.Now || tireInfoB.TbmExpirationTime < DateTime.Now)
        {
            //如果A超期 生成a异常
            if (tireInfoA.TbmExpirationTime < DateTime.Now)
            {
                //送往异常
                abnormalRemarkA = $"胎胚：{productCodeA},超期，下异常";
                WriteLog(abnormalRemarkA, true);
                (abnormalTaskIdA, _) = _Abnormal.CreateCircularAbnormal(productCodeA ?? string.Empty, 0, scannerLocId, abnormalRemarkA);
                return abnormalTaskIdA;
            }
            //如果b超期 给A生成堆垛机任务
            else
            {
                abnormalRemarkA = $"胎胚：{productCodeB},超期";
                WriteLog(abnormalRemarkA, true);
                var materialInfoA = tireInfoA.MaterialCode;
                var materialInfoB = tireInfoB == null ? tireInfoB.MaterialCode : "";
                var noPariTaskId = CreateNoPairTask(scannerLocId, materialInfoA, materialInfoB, productCodeA, productCodeB, tireInfoA);
                return noPariTaskId;
            }


        }

        if (!tireInfoA.InPermit.Equals("Y") || !tireInfoB.InPermit.Equals("Y"))
        {
            //如果A不允许入库 生成a异常
            if (!tireInfoA.InPermit.Equals("Y"))
            {
                //送往异常
                abnormalRemarkA = $"胎胚：{productCodeA},不允许入库，下异常";
                WriteLog(abnormalRemarkA, true);
                (abnormalTaskIdA, _) = _Abnormal.CreateCircularAbnormal(productCodeA ?? string.Empty, 0, scannerLocId, abnormalRemarkA);
                return abnormalTaskIdA;
            }
            //如果B不允许入库 给A生成堆垛机任务
            else
            {
                abnormalRemarkA = $"胎胚：{productCodeB},不允许入库";
                WriteLog(abnormalRemarkA, true);
                var materialInfoA = tireInfoA.MaterialCode;
                var materialInfoB = tireInfoB == null ? tireInfoB.MaterialCode : "";
                var noPariTaskId = CreateNoPairTask(scannerLocId, materialInfoA, materialInfoB, productCodeA, productCodeB, tireInfoA);
                return noPariTaskId;
            }


        }

        if (tireInfoA.TireLock == 1 || tireInfoB.TireLock == 1)
        {
            //如果A锁定 生成a异常
            if (tireInfoA.TireLock == 1)
            {
                //送往异常
                abnormalRemarkA = $"胎胚：{productCodeA},锁定，下异常";
                WriteLog(abnormalRemarkA, true);
                (abnormalTaskIdA, _) = _Abnormal.CreateCircularAbnormal(productCodeA ?? string.Empty, 0, scannerLocId, abnormalRemarkA);
                return abnormalTaskIdA;
            }
            //如果B锁定 给A生成堆垛机任务
            else
            {
                abnormalRemarkA = $"胎胚：{productCodeA},锁定";
                WriteLog(abnormalRemarkA, true);
                var materialInfoA = tireInfoA.MaterialCode;
                var materialInfoB = tireInfoB == null ? tireInfoB.MaterialCode : "";
                var noPariTaskId = CreateNoPairTask(scannerLocId, materialInfoA, materialInfoB, productCodeA, productCodeB, tireInfoA);
                return noPariTaskId;
            }

        }






        var crnInput = _WcsAsrsCrnInput.GetWcsAsrsCrnInputs();

        if (crnInput is null || !crnInput.Any())
        {
            WriteLog("没有可用的入库站台");
            return -7;
        }

        //var netInput = _WcsAsrsNetInput.GetNetInputByStartLoc(locId);
        //if (netInput is null || !crnInput.Any())
        //{
        //    WriteLog("没有可用的入库路线");

        //    return -8;
        //}

        var materialCodeA = tireInfoA.MaterialCode;
        var materialIdA = tireInfoA.MaterialId;
        var materialCodeB = tireInfoB.MaterialCode;
        var materialIdB = tireInfoB.MaterialId;
        if (materialCodeA != materialCodeB)
        {
            ////判断堆垛机是否禁用 如果全部禁用 则下异常
            ////判断缓存是否满 如果缓存满 则下发位等待
            abnormalRemarkA = $"胎胚：{productCodeA}--{productCodeB},物料不一致";
            WriteLog(abnormalRemarkA, true);
            var noPariTaskId = CreateNoPairTask(scannerLocId, materialCodeA, materialCodeB, productCodeA, productCodeB, tireInfoA);
            return noPariTaskId;
        }

        var taskInfoBeanA = new TaskInfoBean();
        var taskInfoBeanB = new TaskInfoBean();
        ProdProductBindingBean productA;
        ProdProductBindingBean productB;
        long taskIdA;
        long taskIdB;
        // 创建绑定关系
        (taskIdA, productA) = GetProductInfo(tireInfoA, scannerLocId, null);
        (taskIdB, productB) = GetProductInfo(tireInfoB, scannerLocId, null);
        var skuIdA = productA.POProdSku?.Id ?? 0;
        var skuIdB = productB.POProdSku?.Id ?? 0;
        DateTime dateTime = DateTime.Now;


        // 直运
        //var isDirectDelivery = _RcsEquipScanner.GetRcsEquipScannerByLocId(scannerLocId)?.IsDirectDelivery ?? 0;
        //if (isDirectDelivery == 1)
        //{
        //    lock (_LockDelivery)
        //    {
        //        var directDeliveryTaskId = DirectDeliveryTask(scannerLocId, materialId, skuId, productCode);
        //        if (directDeliveryTaskId > 0)
        //        {
        //            return directDeliveryTaskId;
        //        }
        //    }
        //}



        //var material = _ProdMaterial.GetMaterialByCode(materialCodeA);
        long targetCrnLoc = GetPairTargetLocM(scannerLocId, materialCodeA);

        if (targetCrnLoc == -1 || targetCrnLoc == -3
            || targetCrnLoc == 0
            || targetCrnLoc == -4 || targetCrnLoc == -5)//  || targetCrnLoc == -2 || targetCrnLoc == -6|| targetCrnLoc == -7
        {
            (abnormalTaskIdA, productA) = _Abnormal.CreateCircularAbnormal(productCodeA, targetCrnLoc, scannerLocId, $"未找到目标位原因：{GetScannerTargetAbnormal(targetCrnLoc)}");
            return abnormalTaskIdA;
        }
        WriteLog($"条码{productCodeA}开始创建任务305", true);
        var remark = string.Empty;
        //没货位了
        if (targetCrnLoc == -6 || targetCrnLoc == -7)
        {
            WriteLog($"条码{productCodeA}--{targetCrnLoc}没货位了", true);
            return targetCrnLoc;
        }
        //没出库路线
        if (targetCrnLoc == -8)
        {
            WriteLog($"条码{productCodeA}--{targetCrnLoc}没入库路线1", true);
            return targetCrnLoc;
        }
        //没出库路线
        if (targetCrnLoc == -2)
        {
            WriteLog($"条码{productCodeA}--{targetCrnLoc}没出库路线2", true);
            return targetCrnLoc;
        }
        WriteLog($"条码{productCodeA}开始创建任务306", true);
        // 没有满足条件输送路线 （进异常）
        if (targetCrnLoc <= 0)
        {
            (abnormalTaskIdA, productA) = _Abnormal.CreateCircularAbnormal(productCodeA, productTaskId, scannerLocId, $"{scannerLocId},未找到目标位原因：{GetScannerTargetAbnormal(targetCrnLoc)}");

            return abnormalTaskIdA;
        }
        WriteLog($"条码{productCodeA}开始创建任务307", true);
        // 判断从开始到目标有没有缓存位
        var cacheLoc = _WcsAsrsNetInput.GetAsrsNetInputByStartLocTargetLoc(scannerLocId, targetCrnLoc);
        //var pairStr = string.Empty;
        //if (isNotPair != 0)
        //{
        //    pairStr = isNotPair == -1 ? $"{isNotPair}没有配对" : isNotPair < 0 ? $"{isNotPair}已配对" : $"{isNotPair}配对输送导致缓存数超限,所以重新查找目标为[{targetCrnLoc}]";
        //}
        WriteLog($"条码{productCodeA}开始创建任务308", true);

        var groupNo = 0;
        // Task表中添加task_group值，并且将原来的输送线任务改为了入库任务
        taskInfoBeanA = _Task.CreateTaskAndCmdGroup(new()
        {
            StartLoc = scannerLocId,
            TargetLoc = targetCrnLoc,
            TargetCmdLoc = cacheLoc == 0 ? targetCrnLoc : cacheLoc,
            Type = (int)EnumTaskType.Transportation,
            GroupNo = groupNo,
            SkuId = skuIdA,
            ScanCount = 1,
            StartTaskType = 1,
            PairStatus = (int)EnumPairStatus.Pairing,
            Remark = $"【创建】| 条码{productCodeA},组号{groupNo}| [{dateTime.ToString("yyyy-MM-dd HH:mm:ss.fff")} ~ {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}]| {remark}",
        });
        groupNo = taskInfoBeanA.POWcsTask.TaskNo ?? 0;
        taskInfoBeanB = _Task.CreateTaskAndCmdGroup(new()
        {
            StartLoc = scannerLocId,
            TargetLoc = targetCrnLoc,
            TargetCmdLoc = cacheLoc == 0 ? targetCrnLoc : cacheLoc,
            Type = (int)EnumTaskType.Transportation,
            GroupNo = groupNo,
            SkuId = skuIdB,
            ScanCount = 1,
            StartTaskType = 1,
            PairStatus = (int)EnumPairStatus.Pairing,
            //| {pairStr}
            Remark = $"【创建】| 条码{productCodeB},组号{groupNo}|[{dateTime.ToString("yyyy-MM-dd HH:mm:ss.fff")} ~ {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}]| {remark}",
        });

        WriteLog($"条码{productCodeA}开始创建任务369", true);
        //记录扫描的物料跟时间 无用 暂时注释
        //_RcsEquipBuilding.AddLineMaterialAndTime(materialId, product.POProdProduct.MdEquipId ?? 0);
        return taskInfoBeanA?.POWcsTask?.Id ?? 0;



    }

    public int CreatePairScanner(long scannerLocId, string productCode)
    {

        var result = _WcsScanner.AddWcsScanner(scannerLocId, productCode, 1, "扫码记录", 0);
        return result;
    }

    public long CreateAbnormalTask()
    {
        // 异常入库
        var locs = _RcsLocation.GetAllLocationsByType((int)EnumLocType.AbnormalInLoc);
        if (locs.Count == 0)
        {
            return 0;
        }
        var loc = locs.FirstOrDefault();
        var abnormalTaskId = loc.TaskId ?? 0;
        var productCode = loc.ProductCode;
        if (!string.IsNullOrWhiteSpace(productCode) && abnormalTaskId == 0)
        {
            var abnoramlLoc = loc.Id ?? 0;
            var remark = loc.Remarks;
            // 未创建
            var taskId = CreateTaskV2(abnoramlLoc, productCode);

            if (taskId > 0)
            {
                _RcsLocation.UpdateLocTaskId(abnoramlLoc, productCode, taskId, $"{productCode}任务已创建{taskId}[{DateTime.Now}] —— {remark}");
            }
        }

        return 0;
    }


    public int ExecuteTask(int taskNo, long locId)
    {
        WriteLog($"任务{taskNo}-执行开始", true);
        var num = 0;
        var task = _Task.GetExecutableTaskByTaskNo(taskNo);
        if (task?.Id is not long taskId)
        {
            return num;
        }
        //异常任务直接删除备份任务和指令
        if (task.TaskType == (int)EnumTaskType.Abnormal)
        {
            num = _Task.UpdateTaskAndCmdStatus(locId, taskId, (int)EnumTaskStatus.Executed);
            _Task.BakAndDeleteTaskAndCmd(task.Id.GetValueOrDefault(), $"异常任务删除");
            _WcsScanner.DeleWcsScannerByTaskId(task.Id ?? 0, "异常任务删除");

        }
        else
        {
            num = _Task.UpdateTaskAndCmdStatus(locId, taskId, (int)EnumTaskStatus.Executed);
        }
        WriteLog($"任务{taskNo}-执行完成", true);
        return num;
    }


    public int FinishTaskCmd(int taskNo, long locId)
    {
        var task = _Task.GetExecutedByTaskNo(taskNo);
        if (task is null || task.TaskType != (int)EnumTaskType.Transportation)
        {
            return 0;
        }
        var taskId = task.Id.GetValueOrDefault();
        // 修改状态
        _TaskCmd.UpdateTaskCmdStatus(locId, taskId, (int)EnumTaskStatus.Finish);
        // 备份删除 lsq注释
        _TaskCmd.BakAndDeleteCmd(taskId);
        _WcsScanner.DeleWcsScannerByTaskId(taskId, "输送任务结束时自动删除");

        return 1;
    }

    public long CreateNoPairTask(long scannerLocId, string materialCodeA, string materialCodeB, string productCodeA, string productCodeB, TireInfo tireInfoA)
    {
        string abnormalRemarkA = "";
        long abnormalTaskIdA = 0;

        DateTime noPariDateTime = DateTime.Now;
        //判断堆垛机是否禁用 如果全部禁用 则下异常
        //判断缓存是否满 如果缓存满 则下发位等待
        var NoPairTarget = GetNoPairTargetLoc(scannerLocId, materialCodeA);
        if (NoPairTarget == -8888)
        {
            abnormalRemarkA = $"胎胚：{productCodeA}物料{materialCodeA}与{productCodeB}物料{materialCodeB}不一致，无可用堆垛机,下异常";
            WriteLog(abnormalRemarkA, true);
            (abnormalTaskIdA, _) = _Abnormal.CreateCircularAbnormal(productCodeA ?? string.Empty, 0, scannerLocId, abnormalRemarkA);
            return abnormalTaskIdA;
        }
        //没货位了
        if (NoPairTarget == -26 || NoPairTarget == -27)
        {
            WriteLog($"胎胚：{productCodeA}物料{materialCodeA}与{productCodeB}物料{materialCodeB}不一致--{NoPairTarget}没货位了", true);
            return NoPairTarget;
        }
        //没入库路线
        if (NoPairTarget == -28)
        {
            WriteLog($"胎胚：{productCodeA}物料{materialCodeA}与{productCodeB}物料{materialCodeB}不一致--{NoPairTarget}没入库路线", true);
            return NoPairTarget;
        }
        //入库路线缓存不满足
        if (NoPairTarget == -22)
        {
            WriteLog($"胎胚：{productCodeA}物料{materialCodeA}与{productCodeB}物料{materialCodeB}不一致--{NoPairTarget}入库路线缓存不满足", true);
            return NoPairTarget;
        }
        //堆垛机持有库位不足
        if (NoPairTarget == -23)
        {
            WriteLog($"胎胚：{productCodeA}物料{materialCodeA}与{productCodeB}物料{materialCodeB}不一致--{NoPairTarget}堆垛机持有库位不足", true);
            return NoPairTarget;
        }
        if (NoPairTarget <= 0)
        {
            WriteLog($"胎胚：{productCodeA}物料{materialCodeA}与{productCodeB}物料{materialCodeB}不一致--{NoPairTarget},未知原因", true);

            return NoPairTarget;
        }
        var noPariGroupNo = 999;//TODO  20250722 在此创建单胎组号999
        var noPariTaskInfoBeanA = new TaskInfoBean();
        ProdProductBindingBean noPariProductA;
        long noPariTaskIdA;
        // 创建绑定关系
        (noPariTaskIdA, noPariProductA) = GetProductInfo(tireInfoA, scannerLocId, null);
        var noPariSkuIdA = noPariProductA.POProdSku?.Id ?? 0;

        noPariTaskInfoBeanA = _Task.CreateTaskAndCmdGroup(new()
        {
            StartLoc = scannerLocId,
            TargetLoc = NoPairTarget,
            TargetCmdLoc = NoPairTarget,
            Type = (int)EnumTaskType.Transportation,
            GroupNo = noPariGroupNo,
            SkuId = noPariSkuIdA,
            ScanCount = 1,
            StartTaskType = 1,
            PairStatus = (int)EnumPairStatus.Pairing,
            Remark = $"【创建】| 条码{productCodeA},组号{noPariGroupNo}| [{noPariDateTime.ToString("yyyy-MM-dd HH:mm:ss.fff")} ~ {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}]",
        });

        WriteLog($"条码{productCodeA}开始创建任务369", true);
        //记录扫描的物料跟时间 无用 暂时注释
        //_RcsEquipBuilding.AddLineMaterialAndTime(materialId, product.POProdProduct.MdEquipId ?? 0);
        return noPariTaskInfoBeanA?.POWcsTask?.Id ?? 0;
    }



    /// <summary>
    /// 获取任务
    /// </summary>
    /// <param name="scannerLocId">当前位置绑定胎胚任务</param>
    /// <param name="productCode">条码任务</param>
    /// <param name="lineTaskNo">线体任务</param>
    /// <returns></returns>
    private (POWcsTask, POWcsTask) GetTasks(long scannerLocId, string productCode)
    {
        // 当前扫描仪最后一次绑定任务
        var locTask = _Task.GetExecutableTaskByLocId(scannerLocId);

        // 当前条码的任务
        var productTask = _Task.GetTaskByProductCode(productCode);

        return (locTask, productTask);
    }






    private (long, ProdProductBindingBean) GetProductInfo(TireInfo tireInfo, long scannerLocId, POWcsTask pOWcsTask)
    {

        var productCode = tireInfo.TireCode;
        // 查找胎胚信息（MES接口）:productCode 数据未找到，或者
        ProdProductBindingBean product;
        var abnormalTaskId = 0L;

        var ptaskId = pOWcsTask?.Id ?? 0;

        // 已绑定未使用
        product = _ProdProduct.GetProductBindingInfoByCode(productCode);

        var skuId = product?.POProdSku?.Id ?? 0;
        if (skuId > 0)
        {

            var task = _BakTask.GetBakTaskBySkuId(skuId);

            if (task is null)
            {
                WriteLog($"获取产品信息：{productCode}产品已绑定未创建任务可以直接使用.", true);
                return (abnormalTaskId, product);
            }
        }

        // 根据MES中间表获取信息
        try
        {

            product = _MesRpc.GetMesProductBind(tireInfo);

        }
        catch (Exception ex)
        {
            WriteLog($"获取MES产品信息发生异常：{ex.ToString()}", true);
        }
        if (product is null)
        {
            (abnormalTaskId, product) = _Abnormal.CreateCircularAbnormal(productCode, ptaskId, scannerLocId, $"{scannerLocId},获取Mes传递的产品信息失败，应获取产品信息的条码为：{productCode}");
        }

        return (abnormalTaskId, product);
    }


    private (long, int, int) GetTargetLocM(long scannerLocId, string materialCode, long materialId, string productCode)
    {
        // 查找条码扫描上次物料
        long targetCrnLoc;
        var groupNo = 0;
        var isNotPair = 0;
        (var task, var oddNoMaterialCrnIds) = _Task.GetLastTaskByLocId(scannerLocId, (int)EnumTaskType.Transportation, materialId);
        //没有找到相同物料的上次任务
        if (task is null)
        {
            WriteLog($"条码{productCode} 没有找到相同物料的上次任务", true);
            isNotPair = -1;
            targetCrnLoc = GetTargetLocV2(scannerLocId, materialId, oddNoMaterialCrnIds, materialCode);
        }
        else
        {
            //未配对的胎
            if (task.TaskGroup == task.TaskNo)
            {
                WriteLog($"条码{productCode} 已找到相同物料的上次任务,且为奇数", true);
                var proSku = _ProSku.GetProdSkuById(task.SkuId);
                var crnInput = _WcsAsrsCrnInput.GetWcsAsrsCrnInputByLoc(task.TargetLoc.GetValueOrDefault());
                var isCount = _WcsAsrsNetInput.IsCrnTaskCounts(crnInput.CrnId.GetValueOrDefault(), crnInput.AreaId.GetValueOrDefault());
                if (proSku != null && proSku.MaterialId == materialId && isCount)
                {
                    ////上次出现同样物料胎胚
                    //var materialHeight = _ProdMaterial.GetMaterialById(proSku.MaterialId ?? 0).MaterialHeight ?? 0;
                    //var wmsSetting = _WmsSetting.GetWmsSettingByCode(ConstFields.MaterialHeightSideType);
                    //_ = int.TryParse(wmsSetting?.Value, out var wmsHeight);
                    ////高胎不配对 (含设置的最小值) 重选货位 防止高胎只剩一个货位
                    //if (materialHeight >= wmsHeight)
                    //{
                    //    targetCrnLoc = GetTargetLocV2(scannerLocId, materialId, oddNoMaterialCrnIds, materialCode);
                    //}
                    //else
                    //{
                    targetCrnLoc = task.TargetLoc.GetValueOrDefault();
                    groupNo = task.TaskNo.GetValueOrDefault();
                    //}

                }
                else
                {
                    WriteLog($"条码{productCode} 已找到相同物料的上次任务,且为偶数", true);
                    isNotPair = task.TaskNo.GetValueOrDefault();
                    // 查找目标位置
                    targetCrnLoc = GetTargetLocV2(scannerLocId, materialId, oddNoMaterialCrnIds, materialCode);
                }
            }
            else
            {
                isNotPair = 0 - (task?.TaskNo ?? 2);
                targetCrnLoc = GetTargetLocV2(scannerLocId, materialId, oddNoMaterialCrnIds, materialCode);
            }
        }
        return (targetCrnLoc, groupNo, isNotPair);
    }

    private long GetPairTargetLocM(long scannerLocId, string materialCode)
    {
        // 查找条码扫描上次物料
        long targetCrnLoc;
        var groupNo = 0;
        targetCrnLoc = GetPairTargetLoc(scannerLocId, materialCode);

        return targetCrnLoc;
    }

    /// <summary>
    /// 给1 2号扫描用
    /// </summary>
    /// <param name="scannerLocId"></param>
    /// <param name="materialId"></param>
    /// <param name="height"></param>
    /// <param name="outside"></param>
    /// <param name="materialCode"></param>
    /// <param name="IsExecute"></param>
    /// <returns></returns>

    private long DirectDeliveryTask(long scannerLocId, long materialId, long skuId, string productCode)
    {
        // 判断是否有直运订单-创建直运订单
        var summary = _WmsOutOrderSummary.GetDirectDeliveryOutOrderSummary(scannerLocId, materialId);
        if (summary is not null)
        {
            var stockNum = _WcsWarehouseCrnMaterial.GetWareHouseStockNoSummaryByMaterialId(summary.MaterialId.GetValueOrDefault());
            if ((stockNum + 1) > (summary.PlanCount - summary.RealCount))
            {
                return 0;
            }

            var targetLoc = summary.TargetLoc.GetValueOrDefault();

            var asrsNetOutput = _WcsAsrsNetOutPut.GetFirstAsrsNetOutputByTargetLoc(targetLoc);
            if (asrsNetOutput?.TranLoc is not long tranLoc)
            {
                return -4;
            }

            // 绑定位置
            _WcsLocSku.SetWcsLocSku(scannerLocId, skuId);

            var summaryId = summary.Id.GetValueOrDefault();
            var masterId = summary.MasterId.GetValueOrDefault();
            // 修改出库订单
            var num = _WmsOutOrderSummary.UpdateSummaryLocAndRealCount(summaryId, scannerLocId, 1);
            if (num == 0)
            {
                return 0;
            }
            //OLDTODO Task表中添加task_group值，并且将原来的输送线任务改为了入库任务
            var taskDirectBean = _Task.CreateTaskAndCmdGroup(new()
            {
                StartLoc = scannerLocId,
                TargetLoc = targetLoc,
                TargetCmdLoc = tranLoc,
                Type = (int)EnumTaskType.Exwarehouse,
                CmdType = (int)EnumTaskType.Transportation,
                SkuId = skuId,
                DirectMasterId = masterId,
                DirectSummaryId = summaryId,
                DirectDelivery = 1,
                Remark = $"直运订单"
            });

            var taskId = taskDirectBean?.POWcsTask?.Id ?? 0;
            if (taskId > 0)
            {
                //同步MES
                SyncMesOnDelivery(productCode, summaryId, targetLoc, scannerLocId);
            }

            return taskId;
        }
        return 0;
    }
    /// <summary>
    /// 同步MES
    /// </summary>
    /// <param name="productCode"></param>
    /// <param name="summaryId"></param>
    /// <param name="targetLoc"></param>
    /// <param name="currentLoc"></param>
    private void SyncMesOnDelivery(string productCode, long summaryId, long targetLoc, long currentLoc)
    {
        // 11.直送信息（WMS->MES）
        var curingRL = _RcsEquipCuring.GetCuringRLByCuringLocId(targetLoc);
        var equipNo = curingRL?.CuringCode;
        var model = curingRL?.Mode;
        var equipModel = _RcsEquipCuring.GetCuringRLByMesModelRL(model);
        _Wms2MesService.PushDirectDelivery(new()
        {
            BarCode = productCode,
            OrderDetailId = summaryId,
            CurrentLoc = currentLoc,
            EquipNo = equipNo,
            EquipModel = equipModel,
        });


    }

    /// <summary>
    /// 先查偶数，在查奇数
    /// </summary>
    /// <param name="scannerLocId">扫描仪id</param>
    /// <param name="materialId">物料id</param>
    /// <param name="height"></param>
    /// <param name="outSide">外径</param>
    /// <param name="materialCode">物料编码(判断是否位NONE)</param>
    /// <param name="oddNoMaterialCrnIds">此扫描仪最近一次任务数位奇数的货叉id</param>
    /// <returns></returns>
    private long GetTargetLoc(long scannerLocId,
        long materialId,
        List<long> oddNoMaterialCrnIds,
        string materialCode)
    {
        var targetLoc = _WcsAsrsNetInput.GetAsrsNetInputByStartLocAndNotInCrnIds(scannerLocId, materialId, oddNoMaterialCrnIds, materialCode);
        return targetLoc;
    }
    private long GetTargetLocV2(long scannerLocId,
    long materialId,
    List<long> oddNoMaterialCrnIds,
    string materialCode)
    {
        long targetLoc = 0;
        targetLoc = _WcsAsrsNetInput.GetAsrsNetInputByStartLocAndNotInCrnIdsV2(scannerLocId, materialId, oddNoMaterialCrnIds, materialCode);
        return targetLoc;
    }
    private long GetPairTargetLoc(long scannerLocId, string materialCode)
    {
        long targetLoc = 0;
        targetLoc = _WcsAsrsNetInput.GetPairAsrsNetInputByStartLocAndNotInCrnIds(scannerLocId, materialCode);
        return targetLoc;
    }
    private long GetNoPairTargetLoc(long scannerLocId, string materialCode)
    {
        long targetLoc = 0;
        targetLoc = _WcsAsrsNetInput.GetNoPairTargetLoc(scannerLocId, materialCode);
        return targetLoc;
    }

    public int CheckInWarehous()
    {
        _ProdProduct.GetWarehousingOutProduct();

        return 0;
    }






    //入库完成推送
    private void SyncMesOnWarehoused(long skuId, long targetLoc)
    {
        //入库完成（WMS->MES）堆垛机入库任务完成后，由WMS将信息推送至MES
        var tireBarcode = _ProdProduct.GetCodeBySkuId(skuId);
        _Wms2MesService.PushWarehousingFinish(tireBarcode, targetLoc.ToString());


        // 胎胚库存
        var location = _RcsLocation.GetLocationById(targetLoc);
        //var hold = _WcsEquipLocHold.GetEquipLocHoldByLoc(location?.Id ?? 0);
        //var crn = _RcsEquipCrn.GetCrnForkById(hold?.Id ?? 0);

        //var warehouseNo = (int)(crn?.WarehourseId ?? 0);
        var warehouseId = _RcsLocation.GetWarehouseByLocationID(targetLoc);
        int.TryParse(warehouseId.ToString(), out var warehouseNo);



    }





}






